var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var user = require('../../utils/user.js');
var check = require('../../utils/check.js');
var wxCharts = require('../../utils/wxcharts.js');
var app = getApp();

// pages/exchange/exchange.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picker1:[],
    picker: ['喵喵喵', '汪汪汪', '哼唧哼唧'],
    cardnum:'',
    name:'',
    address:"",
    phone:"",
    index:''
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },

  getyh() {
    let that = this
    util.request(api.QueryBankList, {

    }, 'GET', {
        "x-merchant-request-code": wx.getStorageSync("code"),
        'Content-Type': 'application/json;charset=UTF-8',
        'token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        var bb = res.data.bankList

        that.setData({
          picker: res.data.bankList
        })
        if(res.code!=200){
          wx.showModal({
            title: '错误提示',
            content: res.msg
          })
        }
      })
  },
  cardnum(e){
    this.setData({
      cardnum: e.detail.value
    })
  },
  name(e){
    this.setData({
      name: e.detail.value
    })
  },
  address(e){
    this.setData({
      address: e.detail.value
    })
  },
  phone(e){
    this.setData({
      phone: e.detail.value
    })
  },


  bangding(){
    if(this.data.cardnum==''){
      wx.showModal({
        title: '错误提示',
        content: '银行卡号不能为空',
        showCancel:false
      })
      return false
    }

    if (this.data.name == '') {
      wx.showModal({
        title: '错误提示',
        content: '开户人姓名不能为空',
        showCancel: false
      })
      return false
    }

    if (this.data.address == '') {
      wx.showModal({
        title: '错误提示',
        content: '开户人地址不能为空',
        showCancel: false
      })
      return false
    }

    if (this.data.phone == '') {
      wx.showModal({
        title: '错误提示',
        content: '请填写银行预留手机号',
        showCancel: false
      })
      return false
    }

    if (this.data.index == '') {
      wx.showModal({
        title: '错误提示',
        content: '选择开户行',
        showCancel: false
      })
      return false
    }

    if (!check.isValidPhone(this.data.phone)) {
      wx.showModal({
        title: '错误信息',
        content: '手机号输入不正确',
        showCancel: false
      });
      return false;
    }


    let that = this
    util.request(api.FillBankInfo, {
        bankCard: that.data.cardnum,
        bankId: that.data.picker[that.data.index].bankId,
        code: wx.getStorageSync("code"),
        name: that.data.name,
        openAddress: that.data.address,
        phone:that.data.phone
    }, 'post', {
        "x-merchant-request-code": wx.getStorageSync("code"),
        'Content-Type': 'application/json;charset=UTF-8',
        'token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        if(res.code==200){
          wx.navigateBack({
            delta: 1
          })
        }
        if(res.code!=200){
          wx.showModal({
            title: '错误提示',
            content: res.msg,
            showCancel:false
          })
        }
      })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getyh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})